home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / rvga01.zip / VGA_T.C < prev    next >
C/C++ Source or Header  |  1994-08-10  |  8KB  |  425 lines

  1. /**********************************************
  2.  
  3.     VGA Routines for TextMode V0.2
  4.     Copyright Reglage (C) 1994
  5.  
  6.     No BIOS Call Guaranteed! :-)
  7.  
  8.     VGA_T.c
  9.  
  10. **********************************************/
  11.  
  12. #include "VGA.h"
  13. #include "VGA_T.h"
  14.  
  15. UBYTE far * T_bscreen=(UBYTE far *) 0xb8000000;
  16. UWORD far * T_screen=(UWORD far *) 0xb8000000;
  17. UWORD T_attr=15<<8;
  18. UBYTE T_width=80;
  19. UWORD T_offset=0;
  20.  
  21. UBYTE    *hexbyte="0123456789ABCDEF";
  22.  
  23. UBYTE T_ColTable[16]={    0x00,0x01,0x02,0x03,
  24.                                 0x04,0x05,0x14,0x07,
  25.                                 0x38,0x39,0x3a,0x3b,
  26.                                 0x3c,0x3d,0x3e,0x3f
  27.                             };
  28.  
  29.  
  30. /**********************************************
  31.  
  32.     Set Screen Start in Memory (Offset)
  33.  
  34. **********************************************/
  35.  
  36. void T_ScreenOffset(UWORD ByteOffset)
  37. {
  38.     T_offset=ByteOffset;
  39.     asm{
  40. //        cli;
  41.  
  42.         mov    dx,0x3d4;
  43.         mov    bx,ByteOffset;
  44.         mov    ah,bh;
  45.         mov    al,0x0c;        //    Register 0Ch, Startaddress High
  46.         out    dx,ax;
  47.  
  48.         mov    ah,bl;
  49.         inc    al;            //    Register 0Dh, Startaddress Low
  50.         out    dx,ax;
  51.  
  52. //        sti;
  53.     }
  54. }
  55.  
  56.  
  57. /**********************************************
  58.  
  59.     Set Number of Columns on Screen
  60.  
  61.     (Virtual Number of Bytes / Scanline)
  62.  
  63. **********************************************/
  64.  
  65. void T_ScreenWidth(UBYTE width)
  66. {
  67.     T_width=width;
  68.     asm{
  69.         mov    ah,width;
  70.         shr    ah,1;
  71.         mov    al,0x13;
  72.         mov    dx,0x3d4;
  73.         out    dx,ax;
  74.     }
  75. }
  76.  
  77. /**********************************************
  78.  
  79.     Set Horizontal PEL Panning Register
  80.  
  81.     >Text mode: no more than 1 character!
  82.  
  83. **********************************************/
  84.  
  85. void T_HorizontalFine(UBYTE X)
  86. {
  87.     V_HorizontalFine(X);
  88. }
  89.  
  90. /**********************************************
  91.  
  92.     Set Palette Color at Index Col
  93.  
  94. **********************************************/
  95.  
  96. void T_SetPal(UBYTE Col, struct Palette *Pal)
  97. {
  98.     V_SetPal(T_ColTable[Col],Pal->R,Pal->G,Pal->B);
  99. }
  100.  
  101.  
  102. /**********************************************
  103.  
  104.     Get Palette Color at Index Col
  105.  
  106. **********************************************/
  107.  
  108. void T_GetPal(UBYTE Col, struct Palette *Pal)
  109. {
  110.     V_GetPal(T_ColTable[Col],&Pal->R,&Pal->G,&Pal->B);
  111. }
  112.  
  113.  
  114. /**********************************************
  115.  
  116.     Set Cursor Type    0: Invisible
  117.                             1:    Full
  118.                             2:    Normal
  119.  
  120.     Change to NO BIOS CALL!
  121.     Let the BIOS library do it's stuff.
  122.  
  123. **********************************************/
  124.  
  125. void T_SetCursor(UBYTE type)
  126. {
  127.     UBYTE cstart, cend;
  128.  
  129.     switch(type)
  130.     {
  131.         case 0:    cstart=0x20;    cend=0;    break;
  132.         case 1:    cstart=0;        cend=7;    break;
  133.         case 2:    cstart=6;        cend=7;    break;
  134.     }
  135.  
  136.     asm{
  137.         mov    ch,cstart;
  138.         mov    cl,cend;
  139.         mov    ah,1;
  140.         int    0x10;
  141.     }
  142. }
  143.  
  144.  
  145. /**********************************************
  146.  
  147.     Set Cursor Position
  148.  
  149.     Bug: Does not work with T_width other than 80!
  150.     Correction: Use VGA ports to change address pointer for Cursor! :)
  151.  
  152. **********************************************/
  153.  
  154. void T_SetCursorPos(UBYTE x,UBYTE y)
  155. {
  156. /*    asm{                        //    VGA BIOS Version :-)
  157.         mov    ah,0x02;
  158.         mov    bh,0x00;
  159.         mov    dl,x;
  160.         mov    dh,y;
  161.         int    0x10;
  162.     }
  163. */
  164.     UWORD    total;
  165.  
  166.     total=x+y*T_width;    //    Calculate Address for Cursor
  167.  
  168.     asm{
  169.         mov    dx,0x3d4;    //    Port 0x3d4
  170.         mov    bx,total;
  171.         mov    ah,bh;
  172.         mov    al,0x0e;        //    index 0x0e: Upper 8 bits of CursorAddr
  173.         out    dx,ax;
  174.         mov    ah,bl;
  175.         mov    al,0x0f;        //    index 0x0f:    Lower 8 bits of CursorAddr
  176.         out    dx,ax;
  177.     }
  178. }
  179.  
  180. /**********************************************
  181.  
  182.     Get Cursor Position
  183.  
  184. **********************************************/
  185.  
  186. void T_GetCursorPos(UBYTE *x,UBYTE *y)
  187. {
  188.     UWORD    total;
  189.  
  190.     asm{
  191.         mov    dx,0x3d4;
  192.         mov    al,0x0e;
  193.         out    dx,al;
  194.         in        al,dx;
  195.         mov    bh,al;
  196.  
  197.         mov    al,0x0f;
  198.         out    dx,al;
  199.         in        al,dx;
  200.         mov    bl,al;
  201.  
  202.         mov    total,bx;
  203.     }
  204.     *y=total/T_width;
  205.     *x=total%T_width;
  206. }
  207.  
  208.  
  209. /**********************************************
  210.  
  211.     Set Height of Characters (0-0x1f)
  212.  
  213. **********************************************/
  214.  
  215. void T_CharHeight(UBYTE height)
  216. {
  217.     asm{
  218.         mov    dx,0x3d4;    //    Inport 0x3d4 index 0x09 ( ->al )
  219.         mov    al,0x09;
  220.         out    dx,al
  221.         in        al,dx;
  222.  
  223.         and    al,0xe0;        //    Mask necessary bits
  224.         or        al,height;    //    Or the height bits
  225.         xchg    al,ah;
  226.         mov    al,0x09;        //    Output to 0x3d4 index 0x09 ( <-ah )
  227.         out    dx,ax;
  228.     }
  229. }
  230.  
  231.  
  232. /**********************************************
  233.  
  234.     Clears _ALL_ Text Screens
  235.  
  236. **********************************************/
  237.  
  238. void T_ClearScreen(UWORD attri)
  239. {
  240.     UWORD i=0;
  241.  
  242.     T_attr=attri<<8;
  243.     while(i<0x7fff)
  244.     {
  245.         T_screen[i]=T_attr;
  246.         i++;
  247.     }
  248. }
  249.  
  250.  
  251. /**********************************************
  252.  
  253.     Outputs a Character at X/Y (should be inline :-( )
  254.  
  255. **********************************************/
  256.  
  257. void T_CharXY(UBYTE tkn,UBYTE x,UBYTE y)
  258. {
  259.     T_screen[x+y*T_width]=T_attr+tkn;
  260. }
  261.  
  262.  
  263. /**********************************************
  264.  
  265.     Get Attribute at X/Y
  266.  
  267. **********************************************/
  268.  
  269. UWORD T_GetAttr(UBYTE x,UBYTE y)
  270. {
  271.     return T_screen[x+y*T_width]&0xff00;
  272. }
  273.  
  274.  
  275. /**********************************************
  276.  
  277.     Draw Box at X1/Y1-X2/Y2 with Style:
  278.                                             0:    Single
  279.                                             1:    Double
  280.  
  281. **********************************************/
  282.  
  283. void T_DrawBox(UBYTE x1,UBYTE y1,UBYTE x2,UBYTE y2,UBYTE style)
  284. {
  285.     UBYTE i;
  286.  
  287.     UWORD far *scr1;
  288.     UWORD far *scr2;
  289.     UWORD temp;
  290.  
  291.     UWORD ly1=y1*T_width;
  292.     UWORD ly2=y2*T_width;
  293.  
  294.     T_screen[x1+ly1]=(style?'╔':'┌')+T_attr;
  295.     T_screen[x2+ly1]=(style?'╗':'┐')+T_attr;
  296.     T_screen[x1+ly2]=(style?'╚':'└')+T_attr;
  297.     T_screen[x2+ly2]=(style?'╝':'┘')+T_attr;
  298.  
  299.     scr1=T_screen+ly1;
  300.     scr2=T_screen+ly2;
  301.     temp=(style?'═':'─')+T_attr;
  302.     for(i=x1+1;i<x2;i++)
  303.     {
  304.         scr1[i]=temp;
  305.         scr2[i]=temp;
  306.     }
  307.     temp=(style?'║':'│')+T_attr;
  308.     for(i=y1+1;i<y2;i++)
  309.     {
  310.         T_screen[x1+i*T_width]=temp;
  311.         T_screen[x2+i*T_width]=temp;
  312.     }
  313. }
  314.  
  315.  
  316. /**********************************************
  317.  
  318.     Draw Filled Box at X1/Y1-X2/Y2 with Style:
  319.                                                     0:    Single
  320.                                                     1:    Double
  321.  
  322. **********************************************/
  323.  
  324. void T_FillBox(UBYTE x1,UBYTE y1,UBYTE x2,UBYTE y2,UBYTE style,UBYTE fillchar)
  325. {
  326.     UBYTE i,y;
  327.  
  328.     UWORD far *scr1;
  329.     UWORD temp;
  330.     UWORD fc=fillchar+T_attr;
  331.  
  332.     UWORD ly1=y1*T_width;
  333.     UWORD ly2=y2*T_width;
  334.  
  335.     T_screen[x1+ly1]=(style?'╔':'┌')+T_attr;    //    Line 1
  336.     scr1=T_screen+ly1;
  337.     temp=(style?'═':'─')+T_attr;
  338.     for(i=x1+1;i<x2;i++)
  339.         scr1[i]=temp;
  340.     T_screen[x2+ly1]=(style?'╗':'┐')+T_attr;
  341.  
  342.     temp=(style?'║':'│')+T_attr;                    //    Lines... |---|
  343.     for(y=y1+1;y<y2;y++)
  344.     {
  345.         scr1=T_screen+y*T_width;
  346.         scr1[x1]=temp;
  347.         for(i=x1+1;i<x2;i++)
  348.             scr1[i]=fc;
  349.         scr1[i]=temp;
  350.     }
  351.  
  352.     T_screen[x1+ly2]=(style?'╚':'└')+T_attr;    //    Last line
  353.     scr1=T_screen+ly2;
  354.     temp=(style?'═':'─')+T_attr;
  355.     for(i=x1+1;i<x2;i++)
  356.         scr1[i]=temp;
  357.     T_screen[x2+ly2]=(style?'╝':'┘')+T_attr;
  358. }
  359.  
  360.  
  361. /**********************************************
  362.  
  363.     Fill Box at X1/Y1-X2/Y2 with T_attr
  364.  
  365. **********************************************/
  366.  
  367. void T_AttrBox(UBYTE x1,UBYTE y1,UBYTE x2,UBYTE y2)
  368. {
  369.     UBYTE far *scr;
  370.     UWORD x,y,lx1,lx2;
  371.     UBYTE attri=T_attr>>8;
  372.  
  373.     lx1=x1*2+1;
  374.     lx2=x2*2+2;
  375.     for(y=y1;y<=y2;y++)
  376.     {
  377.         scr=T_bscreen+y*T_width*2;
  378.         for(x=lx1;x<lx2;x+=2)
  379.             scr[x]=attri;
  380.     }
  381. }
  382.  
  383.  
  384. /**********************************************
  385.  
  386.     Writes string at X/Y
  387.  
  388. **********************************************/
  389.  
  390. void T_String(UBYTE x,UBYTE y,char *string)
  391. {
  392.     UWORD i;
  393.     UWORD far *scr=T_screen+y*T_width+x;
  394.  
  395.     i=0;
  396.     while(string[i])
  397.     {
  398.         scr[i]=string[i]+T_attr;
  399.         i++;
  400.     }
  401. }
  402.  
  403.  
  404. /**********************************************
  405.  
  406.     Display Byte as HEX at X/Y with mode: (see code)
  407.  
  408. **********************************************/
  409.  
  410. void T_OutByte(UBYTE x,UBYTE y,UBYTE byt,UBYTE mode)
  411. {
  412.     UWORD far *scr=&T_screen[y*T_width+x];
  413.  
  414.     if(!byt&&mode)        //    Mode 1 should draw '--' if byt==zero
  415.     {
  416.         scr[0]='-'+T_attr;
  417.         scr[1]='-'+T_attr;
  418.     }
  419.     else
  420.     {
  421.         scr[0]=hexbyte[(byt&0xf0)>>4]+T_attr;
  422.         scr[1]=hexbyte[byt&0x0f]+T_attr;
  423.     }
  424. }
  425.